from typing import List


def check(n, ori, bounds):
    for i in range(len(ori)):
        if ori[i] + n > bounds[i][1] or ori[i] + n < bounds[i][0]:
            return False
    return True


class Solution:
    def countArrays(self, original: List[int], bounds: List[List[int]]) -> int:
        left = 0
        right = int(1e10)
        ans_right = right
        while left <= right:
            mid = (left + right) // 2
            if check(mid, original, bounds):
                ans_right = mid
                left = mid + 1
            else:
                right = mid - 1

        left = 0
        right = int(1e10)
        ans_left = right
        while left <= right:
            mid = (left + right) // 2
            if check(mid, original, bounds):
                right = mid
                while left <= right:
                    mid = (left + right) // 2
                    if check(mid, original, bounds):
                        ans_left = mid
                        right = mid - 1
                    else:
                        left = mid + 1
            else:
                right = mid - 1
        if ans_right == ans_left: return 0
        return ans_right - ans_left + 1


if __name__ == '__main__':
    s = Solution()
    r = s.countArrays(original=[17, 81], bounds=[[36, 58], [67, 110]])
    print(r)
